package com.xin.springboot.controller;


import com.xin.springboot.component.Base64toimage;
import com.xin.springboot.component.ReadTest;
import com.xin.springboot.component.Recipient;

import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;


@RestController
public class ImgController {
    //    public String label;
    @PostMapping(value = "/base64")
    @CrossOrigin(origins = "http://10.100.146.96:8080", maxAge = 5000)
    public HashMap<String, String> img(@RequestBody Recipient recipient) {
        //System.out.println(recipient.getImage());
        Base64toimage transform = new Base64toimage();
        transform.base64toimage(recipient.getImage(), "C:\\Users\\x1893\\Desktop\\tf_model\\123.jpg");
        //System.out.println(Base64ToImage(xin.getImage(), "C:\\Users\\x1893\\Desktop\\tf_model\\123.jpg"));
        try {
            String exe = "python";
            String command = "C:\\Users\\x1893\\Desktop\\tf_model\\Untitled.py";
            String[] cmdArr = new String[]{exe, command};
            Process process = Runtime.getRuntime().exec(cmdArr);
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }

            in.close();
//            int result = process.waitFor();
//            System.out.println("执行结果:" + result);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            Thread.currentThread().sleep(3000);//毫秒
        } catch (Exception e) {
        }
        HashMap<String, String> map = new HashMap<>();
        ReadTest readTest = new ReadTest();
        File file = new File("C:\\Users\\x1893\\Desktop\\tf_model\\label.txt");
        String label = readTest.txt2String(file);
        System.out.println(label);
        map.put("name", label);
        return map;


//        ReadTest readTest = new ReadTest();
//        File file = new File("C:\\Users\\x1893\\Desktop\\tf_model\\label.txt");
//        label = readTest.txt2String(file);


    }


}


